package service;

import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
import dto.PayOrderParam;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: jianghuiyong
 * @Date: 2018/12/4 14:07
 * @Version 1.0
 */
public class ParamValidator {

    /**
     * 创建订单时生成SIGN
     * @param payOrderParam
     * @param signKey
     * @return
     * @throws Exception
     */
    public static String generatorSign(PayOrderParam payOrderParam, String signKey) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("body", payOrderParam.getBody());
        map.put("mchId", payOrderParam.getMchId().toString());
        map.put("outTradeNo", payOrderParam.getOutTradeNo());
        map.put("nonceStr", payOrderParam.getNonceStr());
        map.put("subject", payOrderParam.getSubject());
        map.put("totalFee", payOrderParam.getTotalFee().toString());
        String sign = WXPayUtil.generateSignature(map, signKey, WXPayConstants.SignType.HMACSHA256);
        return sign;
    }

    /**
     * 回调接口验证SIGN
     * @param reqData
     * @param signKey
     * @return
     */
    public static boolean isPayResultNotifySignatureValid(Map<String, String> reqData, String signKey) {
        boolean flag = false;
        try {
            flag = WXPayUtil.isSignatureValid(reqData, signKey, WXPayConstants.SignType.HMACSHA256);
        } catch (Exception e) {
            return false;
        }
        return flag;
    }

}
